/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaCadastroLocacao.java
 *
 * Created on 04/11/2011, 13:30:03
 */
package interfacegrafica;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import modelo.Cliente;
import modelo.Locacao;
import modelo.TipoLocacao;
import modelo.Veiculo;
import org.apache.log4j.Logger;


/**
 *
 * @author samuel
 */
public class JanelaCadastroLocacao extends javax.swing.JFrame {
    private static Logger logger = Logger.getLogger(JanelaCadastroLocacao.class);
    /** Creates new form JanelaCadastroLocacao */
    public JanelaCadastroLocacao() {
        initComponents();
        campoKmSaida.setEnabled(false);
        campoPrevisaoDias.setEnabled(false);
        rotuloLocacaoKmLivre.setEnabled(false);
        rotuloLocacaoporKm.setEnabled(false);
        campoTaxa.setEnabled(false);
        campoPreco.setEnabled(false);
        rotuloPreco.setEnabled(false);
        rotuloTaxa.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoVerificacaoCpf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoVerificacaoPlaca = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        campoKmSaida = new javax.swing.JTextField();
        rotuloKmSaida = new javax.swing.JLabel();
        rotuloTipodaLocacao = new javax.swing.JLabel();
        rotuloPrevisaoDias = new javax.swing.JLabel();
        campoPrevisaoDias = new javax.swing.JTextField();
        rotuloLocacaoporKm = new javax.swing.JRadioButton();
        rotuloLocacaoKmLivre = new javax.swing.JRadioButton();
        botaoCancelarJanelaLocacao = new javax.swing.JButton();
        botaoEnviarJanelaLocacao = new javax.swing.JButton();
        rotuloTeste = new javax.swing.JLabel();
        rotuloTaxa = new javax.swing.JLabel();
        rotuloPreco = new javax.swing.JLabel();
        campoTaxa = new javax.swing.JTextField();
        campoPreco = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Verificação"));

        jLabel1.setText("Digite o cpf:");

        jLabel2.setText("Digite a placa do veículo que deseja alugar:");

        campoVerificacaoPlaca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoVerificacaoPlacaActionPerformed(evt);
            }
        });

        jButton1.setText("Verificar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoVerificacaoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(campoVerificacaoPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(campoVerificacaoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap(61, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(campoVerificacaoPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)))
                .addGap(65, 65, 65))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(141, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro Locação"));

        rotuloKmSaida.setText("Km de Saída");

        rotuloTipodaLocacao.setText("Tipo da locação:");

        rotuloPrevisaoDias.setText("Previsão de dias:");

        campoPrevisaoDias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoPrevisaoDiasActionPerformed(evt);
            }
        });

        buttonGroup1.add(rotuloLocacaoporKm);
        rotuloLocacaoporKm.setText("por Km");
        rotuloLocacaoporKm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotuloLocacaoporKmActionPerformed(evt);
            }
        });

        buttonGroup1.add(rotuloLocacaoKmLivre);
        rotuloLocacaoKmLivre.setText("Km livre");

        botaoCancelarJanelaLocacao.setText("Cancelar");
        botaoCancelarJanelaLocacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarJanelaLocacaoActionPerformed(evt);
            }
        });

        botaoEnviarJanelaLocacao.setText("Enviar");
        botaoEnviarJanelaLocacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoEnviarJanelaLocacaoActionPerformed(evt);
            }
        });

        rotuloTaxa.setText("Taxa base:");

        rotuloPreco.setText("Preço Km:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(rotuloKmSaida)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(campoKmSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(rotuloTipodaLocacao)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(rotuloLocacaoporKm)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(rotuloLocacaoKmLivre)))
                                .addGap(18, 18, 18)
                                .addComponent(rotuloTeste))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(rotuloPrevisaoDias)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoPrevisaoDias, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(rotuloPreco)
                                    .addGap(18, 18, 18)
                                    .addComponent(campoPreco))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(rotuloTaxa)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(campoTaxa, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(223, 223, 223))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(botaoCancelarJanelaLocacao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 385, Short.MAX_VALUE)
                        .addComponent(botaoEnviarJanelaLocacao)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloKmSaida)
                    .addComponent(campoKmSaida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloTipodaLocacao)
                    .addComponent(rotuloLocacaoporKm)
                    .addComponent(rotuloLocacaoKmLivre)
                    .addComponent(rotuloTeste))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloPrevisaoDias)
                    .addComponent(campoPrevisaoDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloTaxa)
                    .addComponent(campoTaxa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloPreco)
                    .addComponent(campoPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoEnviarJanelaLocacao)
                    .addComponent(botaoCancelarJanelaLocacao))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoEnviarJanelaLocacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEnviarJanelaLocacaoActionPerformed
        try {
            rotuloLocacaoporKm.getSelectedObjects();
            int escolha = 0;
            
            if (rotuloLocacaoKmLivre.isSelected()) {
                escolha = 1;
            } else if (rotuloLocacaoporKm.isSelected()) {
                escolha = 2;
            }
         
            Locacao locacao = new Locacao(Integer.parseInt(campoKmSaida.getText().trim()), escolha, Integer.parseInt(campoPrevisaoDias.getText().trim()));
            TipoLocacao tipoLocacao = new TipoLocacao(escolha, Double.parseDouble(campoTaxa.getText()), Double.parseDouble(campoPreco.getText()));

            locacao.setAlugado(true);
            locacao.setFinalizado(false);
            GregorianCalendar cal = new GregorianCalendar();
            locacao.setDiaSaida(cal.get(GregorianCalendar.DAY_OF_MONTH));
            locacao.setMesSaida(cal.get(GregorianCalendar.MONTH + 1));
            locacao.setAnoSaida(cal.get(GregorianCalendar.YEAR));
            locacao.setAnoEntrada(0);
            locacao.setDiaEntrada(0);
            locacao.setMesEntrada(0);
            locacao.setPlaca(campoVerificacaoPlaca.getText());
            
            try {
                locacao.setAlugado(true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            try {
                TelaLogin.em.getTransaction().begin();
                Query query = TelaLogin.em.createQuery("from Veiculo v where v.placa = :placa");
                query.setParameter("placa", campoVerificacaoPlaca.getText());
                Veiculo vec = (Veiculo) query.getSingleResult();
                vec.setDisponivel(false);
                vec.setNumLocacoes(vec.getNumLocacoes() + 1);
                TelaLogin.em.merge(vec);
                TelaLogin.em.getTransaction().commit();
            } catch (NoResultException e) {
                e.printStackTrace();
            }
            
            try {
                TelaLogin.em.getTransaction().begin();
                TelaLogin.em.persist(tipoLocacao);
                Query query = TelaLogin.em.createQuery("from TipoLocacao t");
                List lista = new ArrayList<TipoLocacao>();
                lista = query.getResultList();
                TipoLocacao tipoLoc = (TipoLocacao) lista.get(lista.size() - 1);
                locacao.setTipoLocacao(tipoLoc.getId());
                TelaLogin.em.persist(locacao);
                TelaLogin.em.getTransaction().commit();
                JOptionPane.showMessageDialog(this, "Locação cadastrada com sucesso", "Sucesso", 1);
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, "Erro no cadastro da locação", "Erro", 2);
            }
            this.dispose();
            logger.info("O usuário " + InterfaceGrafica.login + " cadastrou uma locação.");
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastroLocacao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_botaoEnviarJanelaLocacaoActionPerformed

    private void botaoCancelarJanelaLocacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarJanelaLocacaoActionPerformed
        dispose();
    }//GEN-LAST:event_botaoCancelarJanelaLocacaoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (verificarCpfECliente()) {

            campoKmSaida.setEnabled(true);
            campoPrevisaoDias.setEnabled(true);
            rotuloLocacaoKmLivre.setEnabled(true);
            rotuloLocacaoporKm.setEnabled(true);
            campoTaxa.setEnabled(true);
            campoPreco.setEnabled(true);
            rotuloPreco.setEnabled(true);
            rotuloTaxa.setEnabled(true);
            
            
        }
        
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        TelaLogin.em.getTransaction().commit();
        campoVerificacaoCpf.setText("");
        campoVerificacaoPlaca.setText("");
        campoKmSaida.setEnabled(false);
        campoPrevisaoDias.setEnabled(false);
        rotuloLocacaoKmLivre.setEnabled(false);
        rotuloLocacaoporKm.setEnabled(false);
        campoKmSaida.setText("");
        campoPrevisaoDias.setText("");
        campoTaxa.setText("");
        campoPreco.setText("");
        rotuloPreco.setEnabled(false);
        rotuloTaxa.setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void rotuloLocacaoporKmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotuloLocacaoporKmActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rotuloLocacaoporKmActionPerformed

    private void campoVerificacaoPlacaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoVerificacaoPlacaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoVerificacaoPlacaActionPerformed

    private void campoPrevisaoDiasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoPrevisaoDiasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoPrevisaoDiasActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastroLocacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastroLocacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastroLocacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaCadastroLocacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JanelaCadastroLocacao().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCancelarJanelaLocacao;
    private javax.swing.JButton botaoEnviarJanelaLocacao;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JTextField campoKmSaida;
    private javax.swing.JTextField campoPreco;
    private javax.swing.JTextField campoPrevisaoDias;
    private javax.swing.JTextField campoTaxa;
    private javax.swing.JTextField campoVerificacaoCpf;
    private javax.swing.JTextField campoVerificacaoPlaca;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel rotuloKmSaida;
    private javax.swing.JRadioButton rotuloLocacaoKmLivre;
    private javax.swing.JRadioButton rotuloLocacaoporKm;
    private javax.swing.JLabel rotuloPreco;
    private javax.swing.JLabel rotuloPrevisaoDias;
    private javax.swing.JLabel rotuloTaxa;
    private javax.swing.JLabel rotuloTeste;
    private javax.swing.JLabel rotuloTipodaLocacao;
    // End of variables declaration//GEN-END:variables

    private boolean verificarCpfECliente() {
        try {
            TelaLogin.em.getTransaction().begin();
            Query query = TelaLogin.em.createQuery("from Veiculo v where v.placa = :placa");
            query.setParameter("placa", campoVerificacaoPlaca.getText());
            Veiculo vec = (Veiculo) query.getSingleResult();
            if (vec.getDisponivel() == false) {
                JOptionPane.showMessageDialog(this, "Veículo já alugado");
                dispose();
            }
        } catch (NoResultException e) {
            JOptionPane.showMessageDialog(this, "Não há veículos cadastrados com essa placa", "Erro", 0);
            TelaLogin.em.getTransaction().commit();
            return false;
        }

        try {
            Query querycli = TelaLogin.em.createQuery("from Cliente c where c.cpf = :cpf");
            querycli.setParameter("cpf", campoVerificacaoCpf.getText());
            Cliente cli = (Cliente) querycli.getSingleResult();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Não há clientes cadastrados com esse cpf", "Erro", 0);
            TelaLogin.em.getTransaction().commit();
            return false;
        }
        TelaLogin.em.getTransaction().commit();
        return true;

    }
}
